<template>
  <div>
    <!-- 查询 -->
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <el-row :gutter="10" class="mb8">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
              label-width="68px">
              <el-form-item label="团队类型" prop="teamTypeId">
                <el-select v-model="queryParams.teamTypeId" clearable>
                  <el-option label="请选择" />
                  <el-option v-for="dict in dict.type.team_type" :key="dict.value" :label="dict.label"
                    :value="dict.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="团队名称" prop="teamName">
                <el-input v-model="queryParams.teamName" placeholder="请输入" clearable
                  @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="创建人" prop="addName">
                <el-input v-model="queryParams.addName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
              </el-form-item>
            </el-form>
          </el-row>
          <el-row :gutter="10" class="mb9">
            <el-button type="primary" style="width: 120px;" @click="handleQuery">查询</el-button>
            <el-button type="primary" style="width: 120px;" @click="handleAdd"
              v-hasPermi="['team:team:add']">新增</el-button>
          </el-row>
        </div>
      </el-col>
    </el-row>

    <!-- 列表展示 -->
    <el-row :gutter="25">
      <el-col :span="24">
        <div class="grid-content bg-purple">
          <el-table class="biao" v-loading="loading" :data="teamList" border style="width: 100%">
            <el-table-column prop="id" label="序号" width="180" />
            <el-table-column prop="teamName" label="团队名称" width="180" />
            <el-table-column prop="teamTypeId" label="团队类型">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.team_type" :value="scope.row.teamTypeId" />
              </template>
            </el-table-column>
            <el-table-column prop="addName" label="添加人" width="180" />
            <el-table-column prop="createTime" label="添加时间" width="180" />
            <el-table-column prop="desc" label="备注" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" style="color: blue" @click="handleUpdate(scope.row)"
                  v-hasPermi="['team:team:edit']">编辑</el-button>
                <el-button size="mini" type="text" style="color: red" @click="handleDelete(scope.row)"
                  v-hasPermi="['team:team:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改团队管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="团队名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="团队类型" prop="teamTypeId">
          <el-select v-model="form.teamTypeId" placeholder="请选择" style="width: 380px">
            <el-option v-for="dict in dict.type.team_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团队人员">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
            @change="handleCheckAllChange">全选</el-checkbox>
          <div style="margin: 15px 0;"></div>
          <el-checkbox-group v-model="checkedPerson" @change="handleCheckedPersonChange">
            <el-checkbox v-for="person in person" :label="person" :key="person">{{ person }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" prop="desc">
          <el-input v-model="form.desc" type="textarea" placeholder="请输入 (50字)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listTeam, getTeam, delTeam, addTeam, updateTeam } from "@/api/team/team";

const personOptions = ['*三(139****7766)', '*四(139****7766)', '*五(139****7766)', '*六(139****7766)', '*刚(139****7766)', '*丽(139****7766)'];
export default {
  name: "Team",
  dicts: ['team_type'],
  data() {
    return {
      checkAll: false,
      // 默认选中
      checkedPerson: [],
      person: personOptions,
      isIndeterminate: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamName: null,
        teamTypeId: null,
        addName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teamName: [
          { required: true, message: "团队名称不能为空", trigger: "blur" }
        ],
        teamTypeId: [
          { required: true, message: "团队类型不能为空", trigger: "change" }
        ],
        addName: [
          { required: true, message: "添加人不能为空", trigger: "blur" }
        ],
        // teamEmp:[
        //   { required: true, message: "团队人员不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkedPerson = val ? personOptions : [];
      this.isIndeterminate = false;
    },

    /** 查询团队管理列表 */
    getList() {
      this.loading = true;
      listTeam(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamName: null,
        teamTypeId: null,
        addName: null,
        createTime: null,
        desc: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增（编辑）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "新增（编辑）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除团队管理编号为"' + ids + '"的数据项？').then(function () {
        return delTeam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>

<style>
/* 半体 */

.grid-content {
  border-radius: 4px;
  min-height: 36px;
  box-shadow: 0 4px 6px rgba(61, 61, 61, 0.1), 0 -4px 6px rgba(61, 61, 61, 0.1),
    -4px 0 6px rgba(61, 61, 61, 0.1), 2px 0 6px rgba(61, 61, 61, 0.1);
}

.bg-purple {
  background: #ffffff;
  height: 540px;
  margin-top: 15px;
}

/* 按钮 */
.el-row {
  display: flex;
  justify-content: flex-start;
}

.el-button+.el-button,
.el-upload+.el-button {
  margin-left: 10px;
}

/* form表单 */
.custom-select .el-input__inner {
  width: 140px;
}

/* header */
.bg-purple-dark {
  background: #ffffff;
  margin-top: 25px;
  /* height: 100px; */
  padding-top: 10px;
  padding-bottom: 10px;

  box-shadow: 0 4px 6px rgba(61, 61, 61, 0.1), 0 -4px 6px rgba(61, 61, 61, 0.1),
    -4px 0 6px rgba(61, 61, 61, 0.1), 2px 0 6px rgba(61, 61, 61, 0.1);
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.mb8 {
  padding-top: 10px;
  padding-left: 20px;
}

.mb9 {
  padding-left: 20px;
}

.biao {
  padding-top: 20px;
  padding-left: 30px;
  padding-right: 30px
}
</style>
